package com.srwl.mytx.widget;

import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.srwl.mytx.utils.DateUtils;
import com.srwl.mytx.R;
import com.srwl.mytx.widget.datechoose.OnWheelChangedListener;
import com.srwl.mytx.widget.datechoose.OnWheelScrollListener;
import com.srwl.mytx.widget.datechoose.WheelView;
import com.srwl.mytx.widget.datechoose.adapter.AbstractWheelTextAdapter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by dahai2070 on 2016/11/13 0013.
 */

public class AdvertisementDateChooseDialog extends DialogFragment {
    private static final String TAG = "DateChooseDialog";
    @BindView(R.id.wv_date_start)
    WheelView wv_dateStart;
    @BindView(R.id.wv_hour_start)
    WheelView wv_hourStart;
    @BindView(R.id.wv_minute_start)
    WheelView wv_minuteStart;
    @BindView(R.id.wv_date_stop)
    WheelView wv_dateStop;
    @BindView(R.id.wv_hour_stop)
    WheelView wv_hourStop;
    @BindView(R.id.wv_minute_stop)
    WheelView wv_minuteStop;
    //    @BindView(R.id.tv_title)
//    TextView tv_title;
    private CalendarTextAdapter mDateStartAdapter;
    private CalendarTextAdapter mDateStopAdapter;
    private CalendarTextAdapter mHourStartAdapter;
    private CalendarTextAdapter mHourStopAdapter;
    private CalendarTextAdapter mMinuteAdapter;
    private ArrayList<String> arry_date = new ArrayList<String>();
    private ArrayList<String> arry_hour = new ArrayList<String>();
    private ArrayList<String> arry_minute = new ArrayList<String>();
    private int nowDateIndex = 0;//当前时间的索引，以0开始
    private int nowHourId = 0;
    private int nowMinuteId = 0;
    private final int MAX_TEXT_SIZE = 18;
    private final int MIN_TEXT_SIZE = 16;
    private String str_dateStart;
    private String str_hourStart;
    private String str_minuteStart;
    private String str_dateStop;
    private String str_hourStop;
    private String str_minuteStop;
    private DateChooseInterface dateChooseListener;
    Context context;
    private int nowYear;
    private boolean showMinute = false;
    private boolean isRun;
    private int selectStartDate;
    private int selectStopDate;

    public static AdvertisementDateChooseDialog newInstance() {

        // Bundle args = new Bundle();

        AdvertisementDateChooseDialog fragment = new AdvertisementDateChooseDialog();
        // fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
        // int theme = android.R.style.Theme_Holo_Light_Dialog;
        // setStyle(DialogFragment.STYLE_NO_TITLE, theme);
        setStyle(STYLE_NO_TITLE, R.style.RechargeDialogStyle);

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //  return super.onCreateView(inflater, container, savedInstanceState);

        View view = inflater.inflate(R.layout.fragment_date_choose_dialog, container, false);
        ButterKnife.bind(this, view);
        //tv_title.setText("选择时间");
        initData();
        return view;
    }

    //    @Override
//    public void onResume() {
//        super.onResume();
//        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
//    }
    @Override
    public void onStart() {
        super.onStart();
        Window win = getDialog().getWindow(); // 一定要设置Background，如果不设置，window属性设置无效
        // win.setBackgroundDrawable(new ColorDrawable(new ColorDrawable(Color.WHITE)));
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        WindowManager.LayoutParams params = win.getAttributes();
        params.gravity = Gravity.BOTTOM; // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
        // params.width = (int) (dm.widthPixels - 20 * dm.density);
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        win.setAttributes(params);
    }


//
//    @Override
//    public Dialog onCreateDialog(Bundle savedInstanceState) {
//        Dialog dialog = super.onCreateDialog(savedInstanceState);
//
//        dialog.setCanceledOnTouchOutside(true);
//        // 设置宽度为屏宽、靠近屏幕底部。
//        Window window = dialog.getWindow();
//        WindowManager.LayoutParams wlp = window.getAttributes();
//        wlp.gravity = Gravity.BOTTOM;
//        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
//        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
//        window.setAttributes(wlp);
//        return dialog;
//
//    }

    //    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//
//        setContentView(R.layout.fragment_date_choose_dialog);
//        ButterKnife.bind(this);
//
//        tv_title.setText("选择时间");
//        initData();
//    }
//
//    public AdvertisementDateChooseDialog(Context context, boolean showMinute) {
//       // super(context, R.style.date_choose_dialog);
//        super(context);
//        this.context = context;
//        this.showMinute = showMinute;
//    }

    public void setOnDateChooseListener(DateChooseInterface dateChooseListener) {
        this.dateChooseListener = dateChooseListener;
    }

    private void initData() {
        initDateStart();
        initHourStart();
        initDateStop();
        initHourStop();
        if (showMinute) {
            initMinuteStart();
            initMinuteStop();
        }

        initListener();
    }

    /**
     * 初始化日期
     */
    private void initDateStart() {
        Calendar nowCalendar = Calendar.getInstance();
        nowYear = nowCalendar.get(Calendar.YEAR);

        // setDate(nowYear);
        generateDayOfYear();
        selectStartDate = nowDateIndex;
        mDateStartAdapter = new CalendarTextAdapter(context, arry_date, selectStartDate, MAX_TEXT_SIZE, MIN_TEXT_SIZE);
        wv_dateStart.setVisibleItems(5);
        wv_dateStart.setViewAdapter(mDateStartAdapter);
        wv_dateStart.setCurrentItem(selectStartDate);
        wv_dateStart.setCyclic(true);
        str_dateStart = arry_date.get(selectStartDate);
        setTextViewStyle(str_dateStart, mDateStartAdapter);
    }

    private void initDateStop() {
        selectStopDate = nowDateIndex;
        mDateStopAdapter = new CalendarTextAdapter(context, arry_date, selectStopDate, MAX_TEXT_SIZE, MIN_TEXT_SIZE);
        wv_dateStop.setVisibleItems(5);
        wv_dateStop.setViewAdapter(mDateStopAdapter);
        wv_dateStop.setCurrentItem(selectStopDate);
        wv_dateStop.setCyclic(true);
        str_dateStop = arry_date.get(selectStopDate);
        setTextViewStyle(str_dateStart, mDateStopAdapter);
    }

    /**
     * 初始化小时
     */
    private void initHourStart() {
        Calendar nowCalendar = Calendar.getInstance();
        int nowHour = nowCalendar.get(Calendar.HOUR_OF_DAY);
        arry_hour.clear();
        for (int i = 0; i <= 23; i++) {
            arry_hour.add(i + "");
            if (nowHour == i) {
                nowHourId = arry_hour.size() - 1;
            }
        }
        int selectStartHourId = getSelectStartHourId();
        mHourStartAdapter = new CalendarTextAdapter(context, arry_hour, selectStartHourId, MAX_TEXT_SIZE, MIN_TEXT_SIZE);
        wv_hourStart.setVisibleItems(5);
        wv_hourStart.setViewAdapter(mHourStartAdapter);
        wv_hourStart.setCurrentItem(selectStartHourId);
        str_hourStart = arry_hour.get(selectStartHourId) + "";
        setTextViewStyle(str_hourStart, mHourStartAdapter);
    }

    /**
     * 生成开始时间，比当前时间点加一个小时
     *
     * @return
     */
    private int getSelectStartHourId() {
        int selectStartHourId;
        //当前时间点是23点，加一点，就变成明天的零点，
        if (nowHourId == 23) {
            selectStartHourId = 0;
            selectStartDate = addOneDay();
            wv_dateStart.setCurrentItem(selectStartDate);
            str_dateStart = arry_date.get(selectStartDate);
        } else {
            selectStartHourId = nowHourId + 1;
        }
        return selectStartHourId;
    }

    private void initHourStop() {
        int selectStopHourId = getSelectStopHourId();
        mHourStopAdapter = new CalendarTextAdapter(context, arry_hour, selectStopHourId, MAX_TEXT_SIZE, MIN_TEXT_SIZE);
        wv_hourStop.setVisibleItems(5);
        wv_hourStop.setViewAdapter(mHourStopAdapter);
        wv_hourStop.setCurrentItem(selectStopHourId);
        str_hourStop = arry_hour.get(selectStopHourId) + "";
        setTextViewStyle(str_hourStart, mHourStopAdapter);
    }

    /**
     * 生成结束时间，比当前时间点晚2个小时
     *
     * @return
     */
    private int getSelectStopHourId() {
        int selectStopHourId;
        if (nowHourId == 22) {
            //当前时间点等于22点，加2点，就变成明天的零点
            selectStopHourId = 0;
            selectStopDate = addOneDay();
            wv_dateStop.setCurrentItem(selectStopDate);
            str_dateStop = arry_date.get(selectStopDate);
        } else if (nowHourId == 23) {
            //当前时间点等于22点，加2点，就变成明天的1点
            selectStopHourId = 1;
            selectStopDate = addOneDay();
            wv_dateStop.setCurrentItem(selectStopDate);
            str_dateStop = arry_date.get(selectStopDate);
        } else {
            selectStopHourId = nowHourId + 2;
        }
        return selectStopHourId;
    }

    /**
     * 当小时增加后，垮过零点，天数需要增加一天，
     * 增加一天，可能跨年
     */
    // TODO: 2019/5/19  跨年是否应该重新生成日期数据？
    private int addOneDay() {
        int selectDate;
        if (isRun && nowDateIndex == 366) {//闰年跨年的情况
            selectDate = 0;
        } else if (nowDateIndex == 355) { //平年跨年的情况
            selectDate = 0;
        } else {//非跨年的情况，直接加一天
            selectDate = nowDateIndex + 1;
        }
        return selectDate;

    }

    /**
     * 初始化分钟
     */
    private void initMinuteStart() {
        Calendar nowCalendar = Calendar.getInstance();
        int nowMinite = nowCalendar.get(Calendar.MINUTE);
        arry_minute.clear();
        for (int i = 0; i <= 59; i++) {
            arry_minute.add(i + "");
            if (nowMinite == i) {
                nowMinuteId = arry_minute.size() - 1;
            }
        }
        mMinuteAdapter = new CalendarTextAdapter(context, arry_minute, nowMinuteId, MAX_TEXT_SIZE, MIN_TEXT_SIZE);
        wv_minuteStart.setVisibleItems(5);
        wv_minuteStart.setViewAdapter(mMinuteAdapter);
        wv_minuteStart.setCurrentItem(nowMinuteId);
        str_minuteStart = arry_minute.get(nowMinuteId) + "";
        setTextViewStyle(str_minuteStart, mMinuteAdapter);
    }

    private void initMinuteStop() {
        mMinuteAdapter = new CalendarTextAdapter(context, arry_minute, nowMinuteId, MAX_TEXT_SIZE, MIN_TEXT_SIZE);
        wv_minuteStop.setVisibleItems(5);
        wv_minuteStop.setViewAdapter(mMinuteAdapter);
        wv_minuteStop.setCurrentItem(nowMinuteId);
        str_minuteStop = arry_minute.get(nowMinuteId) + "";
        setTextViewStyle(str_minuteStart, mMinuteAdapter);
    }

    /**
     * 初始化滚动监听事件
     */
    private void initListener() {


        //日期********************
        wv_dateStart.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                String currentText = (String) mDateStartAdapter.getItemText(wheel.getCurrentItem());
                setTextViewStyle(currentText, mDateStartAdapter);
//                mDateCalendarTextView.setTextForSendMobileCodeButton(" " + arry_date.get(wheel.getCurrentItem()));
                str_dateStart = arry_date.get(wheel.getCurrentItem());
            }
        });

        wv_dateStart.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                int currentItem = wheel.getCurrentItem();
                str_dateStart = arry_date.get(currentItem);
                checkStartDateRange();


                String currentText = (String) mDateStartAdapter.getItemText(wheel.getCurrentItem());
                setTextViewStyle(currentText, mDateStartAdapter);
            }
        });

        //小时***********************************
        wv_hourStart.addChangingListener(new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                String currentText = (String) mHourStartAdapter.getItemText(wheel.getCurrentItem());
                setTextViewStyle(currentText, mHourStartAdapter);
                str_hourStart = arry_hour.get(wheel.getCurrentItem()) + "";
            }

        });

        wv_hourStart.addScrollingListener(new OnWheelScrollListener() {

            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                int currentItem = wheel.getCurrentItem();
                str_hourStart = arry_hour.get(currentItem);
                checkStartDateRange();
                String currentText = (String) mHourStartAdapter.getItemText(wheel.getCurrentItem());
                setTextViewStyle(currentText, mHourStartAdapter);
            }

        });

//        //分钟********************************************
//        wv_minuteStart.addChangingListener(new OnWheelChangedListener() {
//
//            @Override
//            public void onChanged(WheelView wheel, int oldValue, int newValue) {
//                String currentText = (String) mMinuteAdapter.getItemText(wheel.getCurrentItem());
//                setTextViewStyle(currentText, mMinuteAdapter);
//                str_minuteStart = arry_minute.get(wheel.getCurrentItem()) + "";
//            }
//        });
//
//        wv_minuteStart.addScrollingListener(new OnWheelScrollListener() {
//
//            @Override
//            public void onScrollingStarted(WheelView wheel) {
//
//            }
//
//            @Override
//            public void onScrollingFinished(WheelView wheel) {
//                String currentText = (String) mMinuteAdapter.getItemText(wheel.getCurrentItem());
//                setTextViewStyle(currentText, mMinuteAdapter);
//            }
//        });

        //日期********************
        wv_dateStop.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                String currentText = (String) mDateStopAdapter.getItemText(wheel.getCurrentItem());
                setTextViewStyle(currentText, mDateStopAdapter);
//                mDateCalendarTextView.setTextForSendMobileCodeButton(" " + arry_date.get(wheel.getCurrentItem()));
                str_dateStop = arry_date.get(wheel.getCurrentItem());
            }
        });

        wv_dateStop.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                //选择的日期 小于当前的日期，应该重新选择为当前的日期，不能往后拉
                int currentItem = wheel.getCurrentItem();
                str_dateStop = arry_date.get(currentItem);
                checkStopDateRange();

                String currentText = (String) mDateStopAdapter.getItemText(wheel.getCurrentItem());
                setTextViewStyle(currentText, mDateStopAdapter);
            }
        });

        //小时***********************************
        wv_hourStop.addChangingListener(new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                String currentText = (String) mHourStopAdapter.getItemText(wheel.getCurrentItem());
                setTextViewStyle(currentText, mHourStopAdapter);
                str_hourStop = arry_hour.get(wheel.getCurrentItem()) + "";
            }

        });

        wv_hourStop.addScrollingListener(new OnWheelScrollListener() {

            @Override
            public void onScrollingStarted(WheelView wheel) {


            }

            @Override
            public void onScrollingFinished(WheelView wheel) {

                int currentItem = wheel.getCurrentItem();
                str_hourStop = arry_hour.get(currentItem);
                checkStopDateRange();

                String currentText = (String) mHourStopAdapter.getItemText(wheel.getCurrentItem());
                setTextViewStyle(currentText, mHourStopAdapter);
            }

        });

        //分钟********************************************
        wv_minuteStop.addChangingListener(new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                String currentText = (String) mMinuteAdapter.getItemText(wheel.getCurrentItem());
                setTextViewStyle(currentText, mMinuteAdapter);
                str_minuteStop = arry_minute.get(wheel.getCurrentItem()) + "";
            }
        });

        wv_minuteStop.addScrollingListener(new OnWheelScrollListener() {

            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                String currentText = (String) mMinuteAdapter.getItemText(wheel.getCurrentItem());
                setTextViewStyle(currentText, mMinuteAdapter);
            }
        });


    }

    private void checkStopDateRange() {
        String stopDateString = nowYear + str_dateStop + str_hourStop;
        String format;
        if (showMinute) {
            format = "yyyyMM月dd日HHmm";
        } else {
            format = "yyyyMM月dd日HH";
        }
        Log.i(TAG, "onScrollingStarted: " + format);
        Date currentSelectStopDate = DateUtils.StringToDate(stopDateString, format);
        Date currentDate = new Date();
        Date add2Hour = DateUtils.addHour(currentDate, 2);
        if (currentSelectStopDate.before(add2Hour)) {
            Calendar instance = Calendar.getInstance();
            instance.setTime(add2Hour);
            int hour = instance.get(Calendar.HOUR_OF_DAY);
            int date = instance.get(Calendar.DAY_OF_YEAR);
            wv_hourStop.setCurrentItem(hour);
            wv_dateStop.setCurrentItem(date - 1);
            //当结束的时间超过边界后，把结束边界时间赋值给当前选择时间
            currentSelectStopDate = add2Hour;
        }

        String startDateString = nowYear + str_dateStart + str_hourStart;
        Date currentSelectStartDate = DateUtils.StringToDate(startDateString, format);
        if (currentSelectStopDate.getTime() - currentSelectStartDate.getTime() < 3600000) {
            Date newStartDate = DateUtils.addHour(currentSelectStopDate, -1);
            Calendar instance = Calendar.getInstance();
            instance.setTime(newStartDate);
            int newStartHour = instance.get(Calendar.HOUR_OF_DAY);
            int newStartDay = instance.get(Calendar.DAY_OF_YEAR);
            wv_hourStart.setCurrentItem(newStartHour);
            wv_dateStart.setCurrentItem(newStartDay - 1);
        }

    }

    private void checkStartDateRange() {
        String startDateString = nowYear + str_dateStart + str_hourStart;
        String format = "yyyyMM月dd日HH";
        Log.i(TAG, "onScrollingStarted: " + format);
        Date currentSelectStartDate = DateUtils.StringToDate(startDateString, format);

        Calendar nowCalendar = Calendar.getInstance();
        nowCalendar.add(Calendar.HOUR, 1);
        Date add1Hour = nowCalendar.getTime();
        //选择的时间必须在当前时间的下一个小时之后
        if (currentSelectStartDate.before(add1Hour)) {
            int hour = nowCalendar.get(Calendar.HOUR_OF_DAY);
            int dayOfYear = nowCalendar.get(Calendar.DAY_OF_YEAR);
            wv_hourStart.setCurrentItem(hour);
            wv_dateStart.setCurrentItem(dayOfYear - 1);
            //当开始的时间超过边界后，把开始边界时间赋值给当前选择时间
            currentSelectStartDate = add1Hour;
        }

        //判断当前时间是否在 结束时候之后，如果是，把结束时间 移动到开始时间的后一个小时

        String stopDateString = nowYear + str_dateStop + str_hourStop + str_minuteStop;
        Date stopDate = DateUtils.StringToDate(stopDateString, format);

        //如果结束时间和开始时间的差不足一个小时，需要把结束时间往后延，
        if (stopDate.getTime() - currentSelectStartDate.getTime() < 3600000) {

            Date newStopDate = DateUtils.addHour(currentSelectStartDate, 1);
            Calendar instance = Calendar.getInstance();
            instance.setTime(newStopDate);
            int newStopDay = instance.get(Calendar.DAY_OF_YEAR);
            int newStopHour = instance.get(Calendar.HOUR_OF_DAY);
            wv_hourStop.setCurrentItem(newStopHour);
            wv_dateStop.setCurrentItem(newStopDay - 1);

        }
    }


//    @NonNull
//    @Override
//    public Dialog onCreateDialog(Bundle savedInstanceState) {
//        AlertDialog.Builder builder = new AlertDialog.Builder(context);
//        // Get the layout inflater
//        LayoutInflater inflater = context.getLayoutInflater();
//        View ll_buttonView = inflater.inflate(R.layout.fragment_date_choose_dialog, null);
//        // Inflate and set the layout for the dialog
//        // Pass null as the parent ll_buttonView because its going in the dialog layout
//        builder.setTitle("选择日期");
//
//        builder.setView(ll_buttonView)
//                // Add action buttons
//                .setPositiveButton("确定",
//                        new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onExtendMenuItemClick(DialogInterface dialog, int id) {
//                            }
//                        }).setNegativeButton("取消", null);
//        return builder.create();
//    }

    /**
     * 滚轮的adapter
     */
    private class CalendarTextAdapter extends AbstractWheelTextAdapter {
        ArrayList<String> list;

        protected CalendarTextAdapter(Context context, ArrayList<String> list, int currentItem, int maxsize, int minsize) {
            super(context, R.layout.item_birth_year, R.id.tempValue, currentItem, maxsize, minsize);
            this.list = list;
        }

        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            View view = super.getItem(index, cachedView, parent);
            return view;
        }

        @Override
        public int getItemsCount() {
            return list.size();
        }

        @Override
        protected CharSequence getItemText(int index) {
            String str = list.get(index) + "";
            return str;
        }
    }


    /**
     * 将改年的所有日期写入数组
     *
     * @param year
     */
    private void setDate(int year) {
        arry_date.clear();
        isRun = isRunNian(year);
        Calendar nowCalendar = Calendar.getInstance();
        int nowMonth = nowCalendar.get(Calendar.MONTH) + 1;
        int nowDay = nowCalendar.get(Calendar.DAY_OF_MONTH);
        for (int month = 1; month <= 12; month++) {
            switch (month) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    for (int day = 1; day <= 31; day++) {
                        arry_date.add(month + "月" + day + "日");

                        if (month == nowMonth && day == nowDay) {
                            nowDateIndex = arry_date.size() - 1;
                        }
                    }
                    break;
                case 2:
                    if (isRun) {
                        for (int day = 1; day <= 29; day++) {
                            arry_date.add(month + "月" + day + "日");
                            if (month == nowMonth && day == nowDay) {
                                nowDateIndex = arry_date.size() - 1;
                            }
                        }
                    } else {
                        for (int day = 1; day <= 28; day++) {
                            arry_date.add(month + "月" + day + "日");
                            if (month == nowMonth && day == nowDay) {
                                nowDateIndex = arry_date.size() - 1;
                            }
                        }
                    }
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    for (int day = 1; day <= 30; day++) {
                        arry_date.add(month + "月" + day + "日");
                        if (month == nowMonth && day == nowDay) {
                            nowDateIndex = arry_date.size() - 1;
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }

    private void generateDayOfYear() {
        Calendar instance = Calendar.getInstance();
        nowDateIndex = instance.get(Calendar.DAY_OF_YEAR) - 1;
        int days = instance.getActualMaximum(Calendar.DAY_OF_YEAR);
        instance.get(Calendar.YEAR);
        instance.set(Calendar.MONTH, 0);
        instance.set(Calendar.DATE, 1);

        for (int i = 0; i < days; i++) {
            Date time = instance.getTime();
            String s = DateUtils.dateToString(time, "M月d日");
            arry_date.add(s);
            instance.add(Calendar.DATE, 1);

        }
    }

    /**
     * 判断是否是闰年
     *
     * @param year
     * @return
     */
    private boolean isRunNian(int year) {
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 设置文字的大小
     *
     * @param curriteItemText
     * @param adapter
     */
    public void setTextViewStyle(String curriteItemText, CalendarTextAdapter adapter) {
        ArrayList<View> arrayList = adapter.getTestViews();
        int size = arrayList.size();
        String currentText;
        for (int i = 0; i < size; i++) {
            TextView textvew = (TextView) arrayList.get(i);
            currentText = textvew.getText().toString();
            if (curriteItemText.equals(currentText)) {
                textvew.setTextSize(MAX_TEXT_SIZE);
                textvew.setTextColor(context.getResources().getColor(R.color.black));
            } else {
                textvew.setTextSize(MIN_TEXT_SIZE);
                textvew.setTextColor(context.getResources().getColor(R.color.gray_normal));
            }
        }
    }

    /**
     * 回调选中的时间（默认时间格式"yyyy-MM-dd HH:mm:ss"）
     */
    public interface DateChooseInterface {
        void getDateTime(Date startDate, Date stopDate);
    }
//
//    @OnClick(R.id.back)
//    void back() {
//        dismiss();
//    }
//
//
//    @OnClick(R.id.close)
//    void close() {
//        dismiss();
//    }

    @OnClick(R.id.btn_sure)
    void sure() {
        String startDateString = nowYear + str_dateStart + str_hourStart + str_minuteStart;
        String stopDateString = nowYear + str_dateStop + str_hourStop + str_minuteStop;
        String format;
        if (showMinute) {
            format = "yyyyMM月dd日HHmm";
        } else {
            format = "yyyyMM月dd日HH";
        }
        Date startDate = DateUtils.StringToDate(startDateString, format);
        Date stopDate = DateUtils.StringToDate(stopDateString, format);
        dateChooseListener.getDateTime(startDate, stopDate);
        dismiss();
    }


}
